<?php
declare (strict_types=1);

namespace app\command;

use app\home\model\User;
use app\home\model\UserCoupon;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * Class Coupon
 * 修改 失效优惠券
 */
class Coupon extends Command{
    protected function configure()
    {

        $this->setName('coupon')->setDescription('Coupon');
    }

    /**
     * execute()方法 就是 运行该命令行类的时候要执行的具体业务逻辑代码
     */
    protected function execute(Input $input, Output $output)
    {
        $userCouponModel = new  UserCoupon();
        $userCoupon =  $userCouponModel->where('effective_time','<=',time())->where(['status' => 1])->select();
        $userModel = new User();
        foreach ($userCoupon as  $value){
            $userModel->where(['id' => $value['uid']])->dec('coupon_num')->update();
            $userCouponModel->where(['id' =>$value['id'] ])->where(['status' => 1])->update(['status' =>2 ]);
        }

        $output->writeln('修改成功');
    }

}